package com.yonyou.pmclouds.workflow.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.view.DisableWrap;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.workflow.service.rmiitf.BaseManagerService;
import com.yonyou.pmclouds.workflow.service.rmiitf.IWorkFlowService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Date;


@Slf4j
@RestController
@RequestMapping("/outter/workflow")
public class WorkFlowCallBackController {
    private ObjectMapper mapper = new ObjectMapper();

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private BaseManagerService managerMaintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IWorkFlowService workFlowService;

    @Autowired
    private UserInfoQuery userInfoQuery;

    @DisableWrap
    @RequestMapping("/callback")
    public CallBackResultVO callBack(@RequestBody RequestParamVO paramVO) throws BusinessException {

        log.debug("进入审批回调参数信息：用户编码：" + paramVO.getUserCode() + "业务单据主键：" + paramVO.getBusinessKey() +
                "流程定义key：" + paramVO.getProcessDefinitionKey() + "租户编码：" + paramVO.getTenantCode());

        // 将租户设置到线程上下文中，某些业务单据回掉时会用到
        String pkTenant = paramVO.getTenantCode().substring(9);
        RuntimeEnvironment.setTenantId(pkTenant);

        String pkUser = userInfoQuery.queryUserByCode(paramVO.getUserCode());

        if (StringUtils.isEmpty(pkUser)) {
            CallBackResultVO resultVO = new CallBackResultVO();
            resultVO.setDesc("友工程：审批回调未找到对应的用户");
            resultVO.setMsgSuccess(false);
            return resultVO;
        }

        CallBackResultVO resultVO = new CallBackResultVO();
        boolean callBackResult;
        try {
            callBackResult = workFlowService.approveByCallBack(paramVO.getBusinessKey(), paramVO.getProcessDefinitionKey());
        } catch (BusinessException e) {
            log.error(e.getMessage(), e);
            resultVO.setDesc("友工程：" + e.getMessage());
            resultVO.setMsgSuccess(false);
            return resultVO;
        }

        if (!callBackResult) {
            resultVO.setDesc("友工程：审批回调失败");
            resultVO.setMsgSuccess(false);
            return resultVO;
        }

        resultVO.setDesc("审批成功");
        resultVO.setMsgSuccess(true);
        return resultVO;
    }

}

@Data
class CallBackResultVO implements Serializable {
    private boolean msgSuccess;
    private String desc;
}

@Data
class RequestParamVO implements Serializable {
    private String businessKey;
    private String userCode;
    private String processDefinitionKey;
    private String tenantCode;
}
